package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

import com.dingtao.common.core.WDFragment;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.ui.activity.DynamicMessageActivity;
import com.example.peibei.ui.activity.PersonInfoActivityTwo;
import com.example.peibei.ui.activity.PersonalInfoActivity;
import com.example.peibei.ui.activity.SystemMessageActivity;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuicore.util.ToastUtil;
import com.tencent.qcloud.tuikit.tuiconversation.bean.ConversationInfo;
import com.tencent.qcloud.tuikit.tuiconversation.presenter.ConversationPresenter;
import com.tencent.qcloud.tuikit.tuiconversation.ui.view.ConversationLayout;
import com.tencent.qcloud.tuikit.tuiconversation.ui.view.ConversationListAdapter;
import com.tencent.qcloud.tuikit.tuiconversation.ui.view.ConversationListLayout;

import butterknife.BindView;

public class ChatFragment extends WDFragment {
    @BindView(R.id.conversation_layout)
    ConversationLayout conversation_layout;

    private ConversationPresenter presenter;

    @Override
    public String getPageName() {
        return "消息Fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_chat;
    }

    @Override
    protected void initView() {
        presenter = new ConversationPresenter();
        presenter.setConversationListener();
        conversation_layout.setPresenter(presenter);
        conversation_layout.initDefault();
        ConversationListLayout conversationList = conversation_layout.getConversationList();
        conversationList.setItemAvatarRadius(90);// 设置adapter item头像圆角大小
        conversation_layout.getConversationList().setOnItemClickListener((view, position, conversationInfo) -> {
            String id = conversationInfo.getId();
            if (id.equals("qudan")) {
                Bundle bundle = new Bundle();
                bundle.putInt("type",1);
                intent(SystemMessageActivity.class,bundle);
            } else if(id.equals("personal")){
                Bundle bundle = new Bundle();
                bundle.putInt("type",5);
                intent(SystemMessageActivity.class,bundle);
            }else if (id.equals("dynamic")) {
                Bundle bundle = new Bundle();
                bundle.putInt("type",3);
                intent(DynamicMessageActivity.class,bundle);
            } else if(id.equals("comment")){
                Bundle bundle = new Bundle();
                bundle.putInt("type",4);
                intent(DynamicMessageActivity.class,bundle);
            }else {
                //此处为demo的实现逻辑，更根据会话类型跳转到相关界面，开发者可根据自己的应用场景灵活实现
                startChatActivity(conversationInfo);
            }
        });
        conversation_layout.getConversationList().setOnHeadClickListener((view, position, messageInfo) -> {
            String id = messageInfo.getId();
            if (id.equals("qudan")||id.equals("personal")||id.equals("dynamic")||id.equals("comment")) {
//                    UIUtils.showToastSafe("点击了系统头像");
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("mid", id);
                intent(PersonInfoActivityTwo.class, bundle);
            }
        });
    }

    private void startChatActivity(ConversationInfo conversationInfo) {
        Bundle param = new Bundle();
        param.putInt(TUIConstants.TUIChat.CHAT_TYPE, conversationInfo.isGroup() ? V2TIMConversation.V2TIM_GROUP : V2TIMConversation.V2TIM_C2C);
        param.putString(TUIConstants.TUIChat.CHAT_ID, conversationInfo.getId());
        param.putString(TUIConstants.TUIChat.CHAT_NAME, conversationInfo.getTitle());
        if (conversationInfo.getDraft() != null) {
            param.putString(TUIConstants.TUIChat.DRAFT_TEXT, conversationInfo.getDraft().getDraftText());
            param.putLong(TUIConstants.TUIChat.DRAFT_TIME, conversationInfo.getDraft().getDraftTime());
        }
        param.putBoolean(TUIConstants.TUIChat.IS_TOP_CHAT, conversationInfo.isTop());

        if (conversationInfo.isGroup()) {
            param.putString(TUIConstants.TUIChat.FACE_URL, conversationInfo.getIconPath());
            param.putString(TUIConstants.TUIChat.GROUP_TYPE, conversationInfo.getGroupType());
        }
        if (conversationInfo.isGroup()) {
            TUICore.startActivity(TUIConstants.TUIChat.GROUP_CHAT_ACTIVITY_NAME, param);
        } else {
            TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, param);
        }
    }
}
